﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IndyMosaic.Models.Syndication;

namespace IndyMosaic.Models.Podcast
{
    public class PodcastRepository : IPodcastRepository
    {
        private PodcastDataContext context;

        public PodcastRepository()
        {
            context = new PodcastDataContext();
        }

        #region IPodcastRepository Members

        public Entry GetEntry(int id)
        {
            return context.Entries.Where(e => e.ID == id).SingleOrDefault();
        }

        public IQueryable<Entry> ListAllEntries()
        {
            var entries = from pe in context.Entries
                          where pe.DatePublished != null
                          select pe;

            return entries.OrderByDescending(e => e.DatePublished);
        }

        public IQueryable<Entry> ListLatestTopNEntries(int numberOfEntries)
        {
            var entries = from pe in context.Entries
                          where pe.DatePublished != null
                          select pe;
            return entries.OrderByDescending(e => e.DatePublished).Take(numberOfEntries);
        }

        public string GetFeed(IEnumerable<Entry> entries, string baseUrl, System.Web.Mvc.UrlHelper urlHelper)
        {
            return Feed.GetFeed("Mosaic Church Podcast", "Podcast for Mosaic Church of Indianapolis, IN", entries, baseUrl, urlHelper).GetRss20Feed();
        }

        public void Add(Entry entry)
        {
            context.Entries.InsertOnSubmit(entry);
        }

        public void Save()
        {
            context.SubmitChanges();
        }

        #endregion
    }
}
